package com.jcsz.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jcsz.gulimall.product.vo.BrandVo;
import com.jcsz.gulimall.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcsz.common.utils.PageUtils;
import com.jcsz.common.utils.Query;

import com.jcsz.gulimall.product.dao.CategoryBrandRelationDao;
import com.jcsz.gulimall.product.entity.CategoryBrandRelationEntity;
import com.jcsz.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    public List<CategoryBrandRelationVo> getRelationByBrandId(Long brandId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        List<CategoryBrandRelationVo> categoryBrandRelationVos = categoryBrandRelationEntities.stream().map(obj -> {
            CategoryBrandRelationVo categoryBrandRelationVo = new CategoryBrandRelationVo();
            BeanUtils.copyProperties(obj, categoryBrandRelationVo);
            return categoryBrandRelationVo;
        }).collect(Collectors.toList());
        return categoryBrandRelationVos;
    }

    @Override
    public void updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Override
    public void updateCategoryName(Long catId, String name) {
//
//        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
//        categoryBrandRelationEntity.setCatelogName(name);
//        this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        categoryBrandRelationDao.updateCategoryName(catId, name);
    }

    @Override
    public List<BrandVo> getRelationByCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandVo> brandVos = categoryBrandRelationEntities.stream().map(obj -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(obj, brandVo);
            return brandVo;
        }).collect(Collectors.toList());
        return brandVos;
    }


}